package com.atguigu.guli.service.trade.service.impl;

import com.atguigu.commonutils.JwtInfo;
import com.atguigu.commonutils.ResultCodeEnum;
import com.atguigu.commonutils.result.R;
import com.atguigu.guli.service.base.Exception.GuliException;
import com.atguigu.guli.service.base.dto.CourseDto;
import com.atguigu.guli.service.base.dto.MemberDto;
import com.atguigu.guli.service.trade.entity.Order;
import com.atguigu.guli.service.trade.feign.EduCourseService;
import com.atguigu.guli.service.trade.feign.UcenterMemberService;
import com.atguigu.guli.service.trade.mapper.OrderMapper;
import com.atguigu.guli.service.trade.service.OrderService;
import com.atguigu.guli.service.trade.util.OrderNoUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.OrderUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author wangyang
 * @since 2021-06-15
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private UcenterMemberService ucenterMemberService;


    @Override
    public Order getOrderById(String orderId, JwtInfo jwtInfo) {
        String memberId = jwtInfo.getId();
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("member_id",memberId);
        orderQueryWrapper.eq("id",orderId);

        return baseMapper.selectOne(orderQueryWrapper);
    }

    @Override
    public Boolean isBuyByCourseId(String courseId, String memberId) {
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("member_id",memberId);
        orderQueryWrapper.eq("course_id",courseId);
        orderQueryWrapper.eq("status",1);

        return baseMapper.selectCount(orderQueryWrapper).intValue() > 0;
    }

    @Override
    public String saveOrderByCourseId(String courseId, JwtInfo jwtInfo) {
        //通过courseId与memberId查询订单是否存在,若以及存在则返回订单Id
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("member_id",jwtInfo.getId());
        orderQueryWrapper.eq("course_id",courseId);
        Order order1 = baseMapper.selectOne(orderQueryWrapper);
        if (order1 != null) {
            return order1.getId();
        }

        CourseDto courseDto = eduCourseService.getCourseDtoById(courseId);
        if (courseDto == null) {
            throw new GuliException(ResultCodeEnum.PARAM_ERROR);
        }
        MemberDto memberDto = ucenterMemberService.getMemberDtoByMemberId(jwtInfo.getId());
        if (memberDto == null) {
            throw new GuliException(ResultCodeEnum.PARAM_ERROR);
        }

        //创建订单
        Order order = new Order();
        order.setOrderNo(OrderNoUtils.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseDto.getTitle());
        order.setCourseCover(courseDto.getCover());
        order.setTeacherName(courseDto.getTeacherName());
        order.setTotalFee(courseDto.getPrice().multiply(new BigDecimal(100)));//分
        order.setMemberId(jwtInfo.getId());
        order.setMobile(memberDto.getMobile());
        order.setNickname(memberDto.getNickname());
        order.setStatus(0);//未支付
        order.setPayType(1);//微信支付

        baseMapper.insert(order);
        return order.getId();

    }

    @Override
    public List<Order> listByMemberId(String id) {
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("member_id",id);
        orderQueryWrapper.orderByDesc("gmt_create");
        List<Order> orderList = baseMapper.selectList(orderQueryWrapper);
        return orderList;
    }

    @Override
    public Boolean removeByOrderId(String orderId, String memberId) {
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("member_id",memberId);
        orderQueryWrapper.eq("id",orderId);
        return this.remove(orderQueryWrapper);
    }
}
